package com.srd.phone.bean;

import com.srd.phone.utils.CommonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyAward {


    public int grade;
    public List<GroupData> list = new ArrayList<>(10);


    public static MyAward parseData(String data){

        MyAward myAward = new MyAward();

        String[] categoryStr = new String[] { "红包", "体验劵", "加息劵", "积分" };
        List<GroupData> mList = new ArrayList<>(10);
        try {
            JSONObject jsonObject = new JSONObject(data);

            myAward.grade = jsonObject.optInt("grade");
            for(int i = 0; i < categoryStr.length; i++){
                List<ChildData> childList = new ArrayList<ChildData>();
                GroupData groupData = new GroupData();
                groupData.name = categoryStr[i];
                switch (groupData.name){
                    case "红包":
                        groupData.state = 1;
                        JSONArray array1 = jsonObject.optJSONArray("hchronicle41");
                        for (int j = 0; array1 != null && j < array1.length(); j++) {
                            ChildData item = new ChildData();
                            JSONObject object = array1.getJSONObject(j);
                            item.id = object.optInt("id");
                            item.money = "红包金额" + object.optDouble("money",0) + "元";
                            item.ctime = CommonUtils.formatDataStr(object.optString("ctime"));
                            item.stime = object.optString("stime");
                            item.state = object.optInt("state");
                            item.flag = 1;
                            childList.add(item);


                            // 1 红包   2 体验劵  3  加息劵   4 积分
                        }
                        break;
                    case "体验劵":
                        groupData.state = 2;
                        JSONArray array2 = jsonObject.optJSONArray("hchronicle43");
                        for (int k = 0; array2 != null && k < array2.length(); k++) {
                            ChildData item = new ChildData();
                            JSONObject object = array2.getJSONObject(k);
                            item.id = object.optInt("id");
                            item.money = "体验劵金额" + object.optDouble("money",0) + "元";
                            item.ctime = CommonUtils.formatDataStr(object.optString("ctime"));
                            item.stime = object.optString("stime");
                            item.state = object.optInt("state");
                            item.flag = 1;
                            childList.add(item);
                        }

                        break;
                    case "加息劵":
                        groupData.state = 3;
                        JSONArray array3 = jsonObject.optJSONArray("hchronicle42");
                        for (int l = 0; array3 != null && l < array3.length(); l++) {
                            ChildData item = new ChildData();
                            JSONObject object = array3.getJSONObject(l);
                            item.id = object.optInt("id");
                            item.money = "加息劵利率" + object.optDouble("money",0) + "%";
                            item.ctime = CommonUtils.formatDataStr(object.optString("ctime"));
                            item.stime = object.optString("stime");
                            item.state = object.optInt("state");
                            childList.add(item);
                        }

                        break;
                    case "积分":
                        groupData.state = 4;
                        JSONArray array4 = jsonObject.optJSONArray("yInteRerd");
                        List<ChildData> integralList = new ArrayList<>();
                        ChildData childData = new ChildData();
                        for (int t = 0; array4 != null && t < array4.length(); t++) {
                            ChildData item = new ChildData();
                            JSONObject object = array4.getJSONObject(t);
                            item.id = object.optInt("id");
                            item.reason = object.optString("reason");
                            item.ctime = CommonUtils.formatDataStr(object.optString("ctime"));
                            item.integral = object.optInt("integral");
                            integralList.add(item);
                        }
                        childData.integralList = integralList;
                        childList.add(childData);
                        break;
                }

                groupData.items = childList;
                mList.add(groupData);
            }
            myAward.list = mList;

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return myAward;
    }
}


